package cn.jy.map.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jy.map.domain.AddressJwdMsg;
import cn.jy.map.domain.AddressMsg;
import cn.jy.map.domain.AutoKeyVillage;
import cn.jy.map.domain.JZJSR;
import cn.jy.map.domain.PulpFrequency;
import cn.jy.map.domain.PulpInterval;
import cn.jy.map.domain.PulpWeight;
import cn.jy.map.domain.VillageColligate;
import cn.jy.map.mapper.GjzDzJwdMapper;
import cn.jy.map.mapper.GjzxxMapper;
import cn.jy.map.mapper.VillageColligateMapper;
import cn.jy.map.service.VillageColligateService;
import cn.jy.map.utils.GjzType;
import cn.jy.map.utils.MapUtils;
import cn.jy.nvr.datasource.DataSourceConst;
import cn.jy.nvr.datasource.DataSourceContextHolder;

@Service("villageColligateService")
public class VillageColligateServiceImpl implements VillageColligateService {

	@Autowired
	VillageColligateMapper villageColligateMapper;
	@Autowired
	GjzDzJwdMapper gjzDzJwdMapper;

	Logger logger = Logger.getLogger(VillageColligateServiceImpl.class);

	//	public static void main(String[] args) {
	//		List<String> l1 = new ArrayList<String>();
	//		List<String> l2 = new ArrayList<String>();
	//		
	//		l1.add("1");
	//		l1.add("2");
	//		l1.add("3");
	//		l1.add("4");
	//		
	//		
	//		l2.add("3");
	//		l2.add("4");
	//		l2.add("5");
	//		
	//		l2.removeAll(l1);
	//		
	//		System.out.println(l2);
	//		System.out.println(l1);
	//		
	//	}

	@Override
	public void loadVillageColligateMsg(String villageName) {


		List<VillageColligate> vilList = new ArrayList<VillageColligate>();

		if(villageName != null && !"".equals(villageName)){
			DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
			List<VillageColligate> v = villageColligateMapper.getVillageColligate(villageName);

			VillageColligate vill = null;
			if(vilList != null && vilList.size()>0){
				vill = v.get(0);
				logger.info("查询到村落名称【"+villageName+"】对应的村落综合统计信息 1 条！");
			}else{
				vill = new VillageColligate();
				logger.info("没有查询到村落名称【"+villageName+"】对应的村落综合统计信息，创建一条");
			}
			vilList.add(vill);

		}else{
			//获取所有浆员所在的村落
			DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
			vilList = villageColligateMapper.getAllVillage(MapUtils.allForgeProviderNoStr);
			logger.info("查询到所有村落【"+vilList.size()+"】条！");
		}

		logger.info("----------------------构造村落综合信息开始-----------------------------");
		for(VillageColligate vill : vilList){
			//获取2014-10-01前建档但是后面一直没有来的人，这是之前伪造的浆员
			if(vill == null){
				continue;
			}
			List<String> forgeProviderNo = MapUtils.allForgeProviderNo;
			villageName = vill.getVillageName();
			//获取指定地区下所有的浆员的卡号集合
			DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
			List<String> allProvider = gjzDzJwdMapper.getAllProviderNoByAddress(villageName,MapUtils.getThatYear(MapUtils.getCurrentYear(), -56));
			logger.info("获取村落【"+villageName+"】注册浆员总人数【"+allProvider.size()+"】人");
			//去掉这个村的伪造的浆员
			if(allProvider != null && allProvider.size()>0){
				allProvider.removeAll(forgeProviderNo);
			}

			if(allProvider.size() <= 0){
				continue;
			}

			//			villageName;//村名称
			vill.setVillageName(villageName);
			//			keyManage;//重点管理村
			//			allProviderNum;//注册浆员总人数
			logger.info("获取村落【"+villageName+"】注册浆员总人数【"+allProvider.size()+"】人，除开伪造浆员");
			vill.setAllProviderNum(allProvider.size());

			int lastYearCollNum = 0;//去年献浆总量/袋
			int currentTimePNum = 0;//当期进站人数 
			int currentTimeCollNum = 0;//当期献浆量/袋	
			int lastTimePNum = 0;//同期进站人数	
			int lastTimeCollNum = 0;//同期献浆量/袋	
			int currentTimeAddPNum = 0;//当期新增人数	
			int currentTimeAddCollNum = 0;//当期新增总浆量/袋	
			int lastTimeAddPNum = 0;//同期新增人数	
			int lastTimeAddCollNum = 0;//同期新增总浆量/袋	


			if(allProvider.size() > 2000){
				int num = allProvider.size() / 2000 + 1;
				for(int j=0;j < num;j++){
					int toIndex = 2000 * j + 2000;
					if(toIndex > allProvider.size()){
						toIndex = allProvider.size();
					}
					List<String> subList = allProvider.subList(2000 * j, toIndex);

					//去年献浆总量/袋
					DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
					int tempNum = villageColligateMapper.getCollNumsByProviders(MapUtils.getThatYear(MapUtils.getCurrentYear(), -1) + "-01-01", MapUtils.getThatYear(MapUtils.getCurrentYear(), -1) + "-12-31", subList);
					lastYearCollNum += tempNum;

					//当期进站人数 、当期献浆量/袋	
					Map<String, Integer> tempMap = villageColligateMapper.getcurrentTimeNum(MapUtils.getCurrentYear(),MapUtils.getNowOnlyDateFomat1(),subList);
					int num_1 = tempMap.get("NUM");
					int sumnum_1 = tempMap.get("SUMNUM");

					currentTimePNum += num_1;
					currentTimeCollNum += sumnum_1;

					//同期进站人数 、当期献浆量/袋	
					tempMap = villageColligateMapper.getcurrentTimeNum(MapUtils.getThatYear(MapUtils.getCurrentYear(), -1),MapUtils.getLastYearNow(),subList);
					num_1 = tempMap.get("NUM");
					sumnum_1 = tempMap.get("SUMNUM");

					lastTimePNum += num_1;
					lastTimeCollNum += sumnum_1;

					//当期新增人数、当期新增总浆量/袋	
					tempMap = villageColligateMapper.getcurrentTimeAddNum(MapUtils.getCurrentYear(),MapUtils.getNowOnlyDateFomat1(),subList);
					num_1 = tempMap.get("NUM");
					sumnum_1 = tempMap.get("SUMNUM");

					currentTimeAddPNum += num_1;
					currentTimeAddCollNum += sumnum_1;

					//同期新增人数 、当期献浆量/袋	
					tempMap = villageColligateMapper.getcurrentTimeAddNum(MapUtils.getThatYear(MapUtils.getCurrentYear(), -1),MapUtils.getLastYearNow(),subList);
					num_1 = tempMap.get("NUM");
					sumnum_1 = tempMap.get("SUMNUM");

					lastTimeAddPNum += num_1;
					lastTimeAddCollNum += sumnum_1;
				}

			}else{
				DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
				lastYearCollNum = villageColligateMapper.getCollNumsByProviders(MapUtils.getThatYear(MapUtils.getCurrentYear(), -1) + "-01-01", MapUtils.getThatYear(MapUtils.getCurrentYear(), -1) + "-12-31", allProvider);

				//当期进站人数 、当期献浆量/袋	
				Map<String, Integer> tempMap = villageColligateMapper.getcurrentTimeNum(MapUtils.getCurrentYear(),MapUtils.getNowOnlyDateFomat1(), allProvider);
				currentTimePNum = tempMap.get("NUM");
				currentTimeCollNum= tempMap.get("SUMNUM");

				//同期进站人数 、当期献浆量/袋	
				tempMap = villageColligateMapper.getcurrentTimeNum(MapUtils.getThatYear(MapUtils.getCurrentYear(), -1),MapUtils.getLastYearNow(), allProvider);
				lastTimePNum = tempMap.get("NUM");
				lastTimeCollNum= tempMap.get("SUMNUM");

				//当期新增人数、当期新增总浆量/袋	
				tempMap = villageColligateMapper.getcurrentTimeAddNum(MapUtils.getCurrentYear(),MapUtils.getNowOnlyDateFomat1(),allProvider);
				currentTimeAddPNum = tempMap.get("NUM");
				currentTimeAddCollNum = tempMap.get("SUMNUM");

				//同期新增人数 、当期献浆量/袋	
				tempMap = villageColligateMapper.getcurrentTimeAddNum(MapUtils.getThatYear(MapUtils.getCurrentYear(), -1),MapUtils.getLastYearNow(),allProvider);
				lastTimeAddPNum = tempMap.get("NUM");
				lastTimeAddCollNum = tempMap.get("SUMNUM");

			}



			logger.info("获取村落【"+villageName+"】去年献浆总量【"+lastYearCollNum+"】袋");

			vill.setLastYearCollNum(lastYearCollNum);
			vill.setCurrentTimePNum(currentTimePNum);
			vill.setCurrentTimeCollNum(currentTimeCollNum);
			vill.setLastTimePNum(lastTimePNum);
			vill.setLastTimeCollNum(lastTimeCollNum);

			vill.setCurrentTimeAddPNum(currentTimeAddPNum);
			vill.setCurrentTimeAddCollNum(currentTimeAddCollNum);
			vill.setLastTimeAddPNum(lastTimeAddPNum);
			vill.setLastTimeAddCollNum(lastTimeAddCollNum);


			List<String> fsProvider = MapUtils.retainList(allProvider, MapUtils.allTypeGjzMap.get(GjzType.fuSuGjz));

			vill.setCurrentTimeFuSuPNum(fsProvider.size());//当期复苏献浆员总人数	

			int currentTimeFuSuCollNum = 0;//当期复苏浆员献浆总量/袋	

			if(fsProvider.size() > 2000){
				int num = fsProvider.size() / 2000 + 1;
				for(int j=0;j < num;j++){
					int toIndex = 2000 * j + 2000;
					if(toIndex > fsProvider.size()){
						toIndex = fsProvider.size();
					}
					List<String> subList = fsProvider.subList(2000 * j, toIndex);

					//当期复苏浆员献浆总量/袋	
					DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
					int fsCollNum = villageColligateMapper.getCollNumsByProviders(MapUtils.getCurrentYear()+"-01-01",MapUtils.getNowOnlyDateFomat1(),subList);
					currentTimeFuSuCollNum += fsCollNum;
				}

			}else{
				//当期复苏浆员献浆总量/袋	
				DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
				if(fsProvider.size()>0){
					currentTimeFuSuCollNum = villageColligateMapper.getCollNumsByProviders(MapUtils.getCurrentYear()+"-01-01",MapUtils.getNowOnlyDateFomat1(),fsProvider);
				}
			}

			vill.setCurrentTimeFuSuCollNum(currentTimeFuSuCollNum);

			int currentTimeZCPNum = 0;//忠诚献浆员当期总人数	
			int currentTimeZCCollNum = 0;//忠诚献浆员当期献浆总量	
			int lastTimeZCPNum = 0;//忠诚献浆员同期期总人数	
			int lastTimeZCCollNum = 0;//忠诚献浆员同期献浆总量	

			int lastYearZCCollNum = 0;//忠诚浆员去年全年献浆量	


			if(allProvider.size() > 2000){
				int num = allProvider.size() / 2000 + 1;
				for(int j=0;j < num;j++){
					int toIndex = 2000 * j + 2000;
					if(toIndex > allProvider.size()){
						toIndex = allProvider.size();
					}
					List<String> subList = allProvider.subList(2000 * j, toIndex);
					DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
					//忠诚献浆员当期总人数	、忠诚献浆员当期献浆总量	
					Map<String, Integer> tempMap = villageColligateMapper.getZCAllNums(MapUtils.getThatYear(MapUtils.getCurrentYear(), -1),MapUtils.getCurrentYear(),MapUtils.getNowOnlyDateFomat1(),subList);
					int tempNum = tempMap.get("NUM");
					int tempSumNum = tempMap.get("SUMNUM");

					currentTimeZCPNum += tempNum;
					currentTimeZCCollNum += tempSumNum;

					//忠诚献浆员同期总人数	、忠诚献浆员同期献浆总量	
					tempMap = villageColligateMapper.getZCAllNums(MapUtils.getThatYear(MapUtils.getCurrentYear(), -2),MapUtils.getThatYear(MapUtils.getCurrentYear(), -1),MapUtils.getLastYearNow(),subList);
					tempNum = tempMap.get("NUM");
					tempSumNum = tempMap.get("SUMNUM");

					lastTimeZCPNum += tempNum;
					lastTimeZCCollNum += tempSumNum;

					//忠诚献浆员去年全年献浆总量	
					tempMap = villageColligateMapper.getZCLastYearCOllNums(MapUtils.getThatYear(MapUtils.getCurrentYear(), -1),MapUtils.getCurrentYear(),MapUtils.getNowOnlyDateFomat1(),subList);
					tempSumNum = tempMap.get("SUMNUM");
					lastYearZCCollNum += tempSumNum;
				}

			}else{
				DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
				//忠诚献浆员当期总人数	、忠诚献浆员当期献浆总量	
				Map<String, Integer> tempMap = villageColligateMapper.getZCAllNums(MapUtils.getThatYear(MapUtils.getCurrentYear(), -1),MapUtils.getCurrentYear(),MapUtils.getNowOnlyDateFomat1(),allProvider);
				currentTimeZCPNum = tempMap.get("NUM");
				currentTimeZCCollNum = tempMap.get("SUMNUM");

				//忠诚献浆员同期总人数	、忠诚献浆员同期献浆总量	
				tempMap = villageColligateMapper.getZCAllNums(MapUtils.getThatYear(MapUtils.getCurrentYear(), -2),MapUtils.getThatYear(MapUtils.getCurrentYear(), -1),MapUtils.getLastYearNow(),allProvider);
				lastTimeZCPNum = tempMap.get("NUM");
				lastTimeZCCollNum = tempMap.get("SUMNUM");

				//忠诚献浆员去年全年献浆总量	
				tempMap = villageColligateMapper.getZCLastYearCOllNums(MapUtils.getThatYear(MapUtils.getCurrentYear(), -1),MapUtils.getCurrentYear(),MapUtils.getNowOnlyDateFomat1(),allProvider);
				lastYearZCCollNum = tempMap.get("SUMNUM");

			}

			vill.setCurrentTimeZCPNum(currentTimeZCPNum);
			vill.setCurrentTimeZCCollNum(currentTimeZCCollNum);
			vill.setLastTimeZCPNum(lastTimeZCPNum);
			vill.setLastTimeZCCollNum(lastTimeZCCollNum);
			vill.setLastYearZCCollNum(lastYearZCCollNum);



			int comedNotConnPNum = 0;//进站但未被链接的浆员	
			int comedNotConnThisTimeCollNum = 0;//进站未链接浆员当期献浆量	
			int comedNotConnLastTimeCollNum = 0;//进站未链接浆员同期献浆量	
			int comedNotConnLastYearCollNum = 0;//进站未链接浆员去年献浆总量

			//本村今年进站的浆员
			List<String> comedProvider = MapUtils.retainList(allProvider, MapUtils.allTypeGjzMap.get(GjzType.thisYearComed));
			if(comedProvider.size()>0){
				DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);

				//获取指定浆员中，未被链接的浆员
				List<String> notConnNums = villageColligateMapper.getNotConnProviders(comedProvider);
				comedNotConnPNum = notConnNums.size();
				if(notConnNums.size() > 0){
					DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
					comedNotConnThisTimeCollNum = villageColligateMapper.getCollNumsByProviders(MapUtils.getCurrentYear() + "-01-01", MapUtils.getNowOnlyDateFomat1(), notConnNums);
					comedNotConnLastTimeCollNum = villageColligateMapper.getCollNumsByProviders(MapUtils.getThatYear(MapUtils.getCurrentYear(), -1) + "-01-01", MapUtils.getLastYearNow(), notConnNums);
					comedNotConnLastYearCollNum = villageColligateMapper.getCollNumsByProviders(MapUtils.getThatYear(MapUtils.getCurrentYear(), -1) + "-01-01", MapUtils.getThatYear(MapUtils.getCurrentYear(), -1) + "-12-31", notConnNums);
				}
			}
			vill.setComedNotConnPNum(comedNotConnPNum);
			vill.setComedNotConnThisTimeCollNum(comedNotConnThisTimeCollNum);
			vill.setComedNotConnLastTimeCollNum(comedNotConnLastTimeCollNum);
			vill.setComedNotConnLastYearCollNum(comedNotConnLastYearCollNum);

			DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
			//			currentTimeNotComedPNum;//当期未进站总人数	
			List<String> notComedProviders = villageColligateMapper.getNotComedProviders(MapUtils.getCurrentYear(), MapUtils.getNowOnlyDateFomat1(), allProvider);
			vill.setCurrentTimeNotComedPNum(notComedProviders.size());

			//			currentTimeNotComedPLastYearCollNum;//当期未进站总人数去年献浆总量袋	
			if(notComedProviders.size()>0){
				int currentTimeNotComedPLastYearCollNum = villageColligateMapper.getCollNumsByProviders(MapUtils.getThatYear(MapUtils.getCurrentYear(), -1) + "-01-01", MapUtils.getThatYear(MapUtils.getCurrentYear(), -1) + "-12-31", notComedProviders);
				vill.setCurrentTimeNotComedPLastYearCollNum(currentTimeNotComedPLastYearCollNum);
			}

			//			xiuMianPNum;//休眠浆员总人数	
			List<String> xmProvider = MapUtils.retainList(allProvider, MapUtils.allTypeGjzMap.get(GjzType.xiuMianGjz));
			vill.setXiuMianPNum(xmProvider.size());

			if(xmProvider.size()>0){
				//			lastYearXiuMianCollNum;//休眠浆员去年献浆总量/袋	
				int lastYearXiuMianCollNum = villageColligateMapper.getCollNumsByProviders(MapUtils.getThatYear(MapUtils.getCurrentYear(), -1) + "-01-01", MapUtils.getThatYear(MapUtils.getCurrentYear(), -1) + "-12-31", xmProvider);
				//			lastTimeXiuMianCollNum;//休眠浆员同期献浆总量/袋	
				int lastTimeXiuMianCollNum = villageColligateMapper.getCollNumsByProviders(MapUtils.getThatYear(MapUtils.getCurrentYear(), -1) + "-01-01", MapUtils.getLastYearNow(), xmProvider);

				vill.setLastYearXiuMianCollNum(lastYearXiuMianCollNum);
				vill.setLastTimeXiuMianCollNum(lastTimeXiuMianCollNum);

			}

			//			connXMPNum;//已链接休眠的献浆员	
			List<String> connXMProvider = MapUtils.retainList(allProvider, MapUtils.allTypeGjzMap.get(GjzType.xiuMianBeConnect));
			vill.setConnXMPNum(connXMProvider.size());
			if(connXMProvider.size()>0){
				//			lastTimeConnXMCollNum;//已链接休眠的献浆员同期献浆总量/袋	
				int lastTimeConnXMCollNum = villageColligateMapper.getCollNumsByProviders(MapUtils.getThatYear(MapUtils.getCurrentYear(), -1) + "-01-01", MapUtils.getLastYearNow(), connXMProvider);
				vill.setLastTimeConnXMCollNum(lastTimeConnXMCollNum);
			}

			//			notConnXMPNum;//休眠中未链接献浆员	
			List<String> notConnXMProvider = MapUtils.retainList(allProvider, MapUtils.allTypeGjzMap.get(GjzType.xiuMianNotConnect));
			vill.setNotConnXMPNum(notConnXMProvider.size());
			if(notConnXMProvider.size()>0){
				//			lastTimeNotConnXMCollNum;//休眠中未链接献浆员同期献浆总量/袋	
				int lastTimeNotConnXMCollNum = villageColligateMapper.getCollNumsByProviders(MapUtils.getThatYear(MapUtils.getCurrentYear(), -1) + "-01-01", MapUtils.getLastYearNow(), notConnXMProvider);
				vill.setLastTimeNotConnXMCollNum(lastTimeNotConnXMCollNum);
			}

			//			liuShiPNum;//流失浆员总人数	
			List<String> lsProvider = MapUtils.retainList(allProvider, MapUtils.allTypeGjzMap.get(GjzType.liuShiGjz));
			vill.setLiuShiPNum(lsProvider.size());

			DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
			//			connNurseNum;//管理护士总人数
			int connNurseNum = villageColligateMapper.getManageNurseNum(allProvider);
			vill.setConnNurseNum(connNurseNum);
			//			connedPNum;//已链接献浆员总人数	
			int connedPNum = villageColligateMapper.getConnedPNum(allProvider);
			vill.setConnedPNum(connedPNum);
			//			VolunteerNum;//义工总人数	
			int volunteerNum = villageColligateMapper.getVolunteerNum(allProvider);
			vill.setVolunteerNum(volunteerNum);
			//			VolunConnGJZNum;//义工管理浆员总人数
			int volunConnGJZNum = villageColligateMapper.getVolunConnGJZNum(allProvider);
			vill.setVolunConnGJZNum(volunConnGJZNum);



		}
		logger.info("----------------------构造村落综合信息结束-----------------------------");

		for(VillageColligate vill : vilList){
			DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
			if(vill == null){
				continue;
			}
			VillageColligate vc = MapUtils.allVillageColligate.get(vill.getVillageName());

			if(vc != null){
				vill.setId(vc.getId());
				villageColligateMapper.updateVillageColligate(vill);
				logger.info("更新村落【"+vill.getVillageName()+"】综合统计信息成功！");
			}else{
				vill.setId(UUID.randomUUID().toString());
				villageColligateMapper.saveVillageColligate(vill);
				logger.info("创建村落【"+vill.getVillageName()+"】综合统计信息成功！");
			}
			logger.info("村落【"+vill.getVillageName()+"】义工人数【"+vill.getVolunteerNum()+"】！");
			MapUtils.allVillageColligate.put(vill.getVillageName(), vill);
		}


	}

	@Override
	public List<VillageColligate> getVillageColligateMsg(String villageName) {
		logger.info("获取村落【"+villageName+"】综合统计信息！");
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		return villageColligateMapper.getVillageColligate(villageName);

	}

	@Override
	public List<VillageColligate> getVillageColligateByDate(String date) {
		// 查询当天进站的所有浆员
		DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
		List<String> providerNoList = gjzDzJwdMapper.getAllProviderNoByDate(date);

		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		List<String> dzList = gjzDzJwdMapper.getDzJwdByProviderNo(providerNoList);

		List<VillageColligate> vills = new ArrayList<VillageColligate>();
		if(dzList != null){
			for (String dz : dzList) {
				VillageColligate vill = MapUtils.allVillageColligate.get(dz);
				if(vill != null){
					vills.add(vill);
				}
			}
		}

		return vills;
	}

	@Override
	public void setKeyVillage(List<VillageColligate> vills) {
		if(vills != null){
			//先将原来所有是重点村的设置为不是重点村，然后全部重新设置
			DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
			//			villageColligateMapper.cleanKeyVillage();

			villageColligateMapper.setKeyVillage(vills);
		}

	}

	@Override
	public List<VillageColligate> getTotalVillageColligate() {
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		return villageColligateMapper.getTotalVillageColligate();
	}

	@Override
	public Map<String,List<AddressMsg>> getAutoKeyVill() {
		logger.info("查询所有村落，包含系统自动分配的重点村的地址经纬度信息！！");
		Map<String,List<AddressMsg>> map = new HashMap<String,List<AddressMsg>>();
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		List<VillageColligate> keyVillage = villageColligateMapper.getAllKeyAutoVillage(true);

		List<AddressMsg> amsA = new ArrayList<AddressMsg>(); 
		List<AddressMsg> amsB = new ArrayList<AddressMsg>(); 
		List<AddressMsg> amsC = new ArrayList<AddressMsg>(); 

		if(keyVillage != null){
			for (VillageColligate vill : keyVillage) {
				logger.info("重点村落名称："+vill.getVillageName());
				AddressMsg am = MapUtils.allDzJwd.get(vill.getVillageName());
				if(am == null){
					continue;
				}
				if(vill.isKeyManageA()){
					amsA.add(am);
				}
				if(vill.isKeyManageB()){
					amsB.add(am);
				}
				if(vill.isKeyManageC()){
					amsC.add(am);
				}
			}
		}


		map.put("kmA", amsA);
		map.put("kmB", amsB);
		map.put("kmC", amsC);

		return map;
	}


	@Override
	public List<VillageColligate> getAllAutoKeyVill() {
		logger.info("查询所有村落，包含系统自动分配的重点村！！");
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		return villageColligateMapper.getAllKeyAutoVillage(false);
	}

	@Override
	public void setAutoKeyVillage() {
		logger.info("自动设置不同类型的重点村落   开始");
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);

		villageColligateMapper.delAutoKeyVillage();

		setAutoKeyVill("A");
		setAutoKeyVill("B");
		setAutoKeyVill("C");

		logger.info("自动设置不同类型的重点村落   结束");
	}

	private void setAutoKeyVill(String type){
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		List<VillageColligate> list = null;//villageColligateMapper.getSortDiffByType(type);

		if("A".equals(type)){
			list = villageColligateMapper.getAddPNum(3);
			List<VillageColligate> temp = villageColligateMapper.getZcDown(3);
			if(list != null && temp != null && temp.size()>0){
				list.addAll(temp);
			}

		}else if("B".equals(type)){
			list = villageColligateMapper.getSortDiffCollNum(false);
		}else if("C".equals(type)){
			list = villageColligateMapper.getSortDiffCollNum(true);
		}


		if(list != null){
			for (VillageColligate vill : list) {
				AutoKeyVillage akv = new AutoKeyVillage();
				akv.setId(UUID.randomUUID().toString());
				akv.setKeyVillType(type);
				akv.setVillageName(vill.getVillageName());

				villageColligateMapper.saveAutoKeyVillage(akv);
				logger.info("自动设置村落【"+vill.getVillageName()+"】为【"+type+"】类型的重点村落");
			}
		}
	}


	@Override
	public Map<String, Object> getPulpWeight(String startTime, String endTime) {
		DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
		SimpleDateFormat df=new SimpleDateFormat("yyyy");		
		String newDate=df.format(new Date());
		Map<String, Object> map=new HashMap<String, Object>();
		List<Object> list=new ArrayList<Object>();
		int tmp=0;
		int tmpTotal=0;
		//18年献浆统计


		//int count=0;
		for (int j = 2016; j <= Integer.valueOf(newDate); j++) {
			startTime=j+"-01-01";
			endTime=j+"-01-31";
			tmp=0;

			for (int i = 0; i <12; i++) {
				int pulps=Integer.valueOf(villageColligateMapper.getPulpWeight(MapUtils.getThatDate(startTime, i, 2), MapUtils.getThatDate(endTime,i, 2)));
				List<PulpFrequency> tmpList=villageColligateMapper.getCreateDate(MapUtils.getThatDate(startTime, i, 2), MapUtils.getThatDate(endTime,i, 2));
				tmpTotal=0;
				for (int k = 0; k < tmpList.size(); k++) {
					if (tmpList.get(k).getSecond()<2016) {
						tmpTotal+=tmpList.get(k).getTotal();
					}else if (tmpList.get(k).getSecond()==j) {
						if (j==2016) {
							map.put(j+"CreateDate"+i, tmpList.get(k).getTotal());//16年建档人数
						}
						if (j>2016) {
							for (int k2 = k-(j-2016); k2 <=k ; k2++) {
								map.put(j+"Create"+(k-k2)+"Date"+i, tmpList.get(k2).getTotal());//17年中16年建档和17年建档的人数        18年 17年建档  16年建档 18年建档人数

							}							
						}
					}
				}


				map.put(j+"otherCreateDate"+i,tmpTotal);//16\17\18年每个月建档日期小于2016年的
				tmp+=pulps;
				map.put(j+"YearPulp"+i, pulps);//j年献浆量
				map.put(j+"TotalPulp"+i,tmp);//j年的每个月累计浆量
				list.add(map);

			}
		}



		//		map.put("result", list);



		return map;
	}


	@Override
	public Map<String, Object> getNewPeople(String startTime, String endTime) {
		List<PulpWeight> list=new ArrayList<PulpWeight>();
		DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
		SimpleDateFormat df=new SimpleDateFormat("yyyy");		
		String newDate=df.format(new Date());
		Map<String, Object> map=new HashMap<String, Object>();
		int newPulpsTmp =0;
		int RTypePulpsTmp=0;
		int NotPulpsTmp=0;
		for (int j = 2016; j <=Integer.valueOf(newDate); j++) {
			startTime=j+"-01-01";
			endTime=j+"-01-31";
			newPulpsTmp=0;
			for (int i = 0; i <12; i++) {	
				String newPulps= villageColligateMapper.getNewPeople(MapUtils.getThatDate(startTime, i, 2), MapUtils.getThatDate(endTime, i, 2));
				String RTypePulps=villageColligateMapper.getRTypePeople(MapUtils.getThatDate(startTime, i, 2), MapUtils.getThatDate(endTime, i, 2));
				String NotPulps=villageColligateMapper.getNotPulpPeople(MapUtils.getThatDate(startTime, i, 2), MapUtils.getThatDate(endTime, i, 2));
				newPulpsTmp+=Integer.valueOf(newPulps);
				RTypePulpsTmp+=Integer.valueOf(RTypePulps);
				NotPulpsTmp+=Integer.valueOf(NotPulps);

				map.put(j+"newPulps"+i,newPulps);
				map.put(j+"RTypePulps"+i,RTypePulps);
				map.put(j+"NotPulps"+i,NotPulps);

				map.put(j+"TotalnewPulps"+i,newPulpsTmp);
//				map.put(j+"TotalRTypePulps"+i,RTypePulpsTmp);
//				map.put(j+"TotalNotPulps"+i,NotPulpsTmp);
			}
		}





		return map;
	}







	@Override
	public Map<String, Object> getByOutProvider() {		
		DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
		SimpleDateFormat df=new SimpleDateFormat("yyyy");
		String newDate=df.format(new Date());
		
		Map<String, Object> map=new HashMap<String, Object>();

		int tmp=0;
		int tmp1=0;
		int tmp2=0;
		int tmp3=0;


		for (int j = 2016; j <= Integer.valueOf(newDate); j++) {
			String date=j+"-01-01";
			String date1=j+"-01-31";
			String startTime=MapUtils.getThatDate(date, -56, 1);
			String endTime=MapUtils.getThatDate(date1, -56, 1);
			tmp=0;
			tmp1=0;
			tmp2=0;
			tmp3=0;
			int allProvideNum=villageColligateMapper.getByAllProviderNum(MapUtils.getThatDate(startTime, 0, 2), MapUtils.getThatDate(date1, 0, 2));
			for (int i = 0; i < 12; i++) {
				Integer outProvider=villageColligateMapper.getByOutProvider(MapUtils.getThatDate(startTime, i, 2),MapUtils.getThatDate(endTime, i, 2));
				int newAddProvider=Integer.valueOf(villageColligateMapper.getNewPeople(MapUtils.getThatDate(date, i, 2), MapUtils.getThatDate(date1, i, 2)));
				int pulp=Integer.valueOf(villageColligateMapper.getPulpWeight(MapUtils.getThatDate(date, i, 2),MapUtils.getThatDate(date1, i, 2)));
				tmp3+=pulp;
				Integer jz=newAddProvider-outProvider;
				map.put(j+"netGrowth"+i,newAddProvider-outProvider);		//净增长	
				tmp+=outProvider;
				tmp1+=newAddProvider;
				tmp2+=jz;
				map.put(j+"ndttlj"+i,tmp);//年度淘汰浆员
				map.put(j+"ndxzlj"+i, tmp1);//年度新增浆员
				map.put(j+"outProvider"+i,outProvider);//淘汰浆员
				map.put(j+"ndjzlj"+i, tmp2);//年度净增涨
				map.put(j+"pulp"+i, tmp3);

				if (i>0) {
					map.put(j+"zcrs"+i, allProvideNum+jz);//本月在册人数
				}else {
					map.put(j+"zcrs"+i, allProvideNum);
				}

			}
		}




		/*tmp=0;
		tmp1=0;
		tmp2=0;
		tmp3=0;
		for (int j = 0; j < 12; j++) {

			Integer outProvider=villageColligateMapper.getByOutProvider(MapUtils.getThatDate(startTime1, j, 2),MapUtils.getThatDate(endTime1, j, 2));
			int newAddProvider=Integer.valueOf(villageColligateMapper.getNewPeople(MapUtils.getThatDate(lastYear,j , 2), MapUtils.getThatDate(lastYear1, j, 2)));
			int pulp=Integer.valueOf(villageColligateMapper.getPulpWeight(MapUtils.getThatDate(lastYear, j, 2),MapUtils.getThatDate(lastYear1, j, 2)));
			Integer jz=newAddProvider-outProvider;
			map.put("netGrowthLast"+j,newAddProvider-outProvider);		//净增长	
			tmp+=outProvider;
			tmp1+=newAddProvider;
			tmp2+=jz;
			tmp3+=pulp;
			map.put("ndttljLast"+j,tmp);//年度淘汰浆员
			map.put("ndxzljLast"+j, tmp1);//年度新增浆员
			map.put("outProviderLast"+j,outProvider);//淘汰浆员
			map.put("ndjzljLast"+j, tmp2);//年度净增涨

			if (j>0) {
				map.put("zcrsLast"+j, allProvideNumLast+jz);//本月在册人数
			}else {
				map.put("zcrsLast"+j, allProvideNumLast);
			}

		}*/
		return map;
	}


	@Override
	public Map<String, Object>getByJSRProvider(String startTime,String endTime) {
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		SimpleDateFormat df=new SimpleDateFormat("yyyy");
		String date=df.format(new Date())+"-01-01";
		String[] luyArray={"后向","马晓","马小","包志","后书","杨全","杜志","闫志","高同","杨金"};
		List<JZJSR> list=new ArrayList<JZJSR>();
		Map<String, Object> map=new HashMap<String, Object>();
		list.addAll(villageColligateMapper.getByJSRProvider(startTime,endTime));
		list.addAll(villageColligateMapper.getByJSRProviderMonth(date,endTime));
		for (int i = 0; i < luyArray.length; i++) {
			list.addAll(villageColligateMapper.getNewProviderPulp(luyArray[i],endTime));
		}
		map.put("jsrPulp", list);
		return map;

	}


	@Override
	public List<PulpInterval> getPulpInterval() {
		DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
		List<PulpInterval> list =villageColligateMapper.getPulpInterval();
		int count=0;
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
		for (int i = 0; i < list.size()-1; i++) {
			if (list.get(i).getProviderNo().equals(list.get(i+1).getProviderNo())) {
				try {
//					
					if (((df.parse(list.get(i+1).getCollectionDate()).getTime()-df.parse(list.get(i).getCollectionDate()).getTime())/(24*60*60*1000))<18) {
						System.out.println(((df.parse(list.get(i+1).getCollectionDate()).getTime()-df.parse(list.get(i).getCollectionDate()).getTime())/(24*60*60*1000)));
						count+=1;
					}
				} catch (ParseException e) {
					
					e.printStackTrace();
				}
			}
		}
		System.out.println(count);
		return null;
	}




}
